import { _decorator, Component, EventTouch, Input, instantiate, Label, Node, UITransform, Vec2, Vec3 } from 'cc';
import { AssetList, Global } from '../Global';
import { CardType } from './BaseCard';
import { CardLoad } from './CardLoad';
import { CatCardLoad } from './CatCardLoad';
import { UIMgr } from '../Mgr/UIMgr';
import { AdMgr } from '../Mgr/AdMgr';
import { BattleCard } from './BattleCard';
import { LocalStorageMgr } from '../Mgr/LocalStorageMgr';
import { GameGuide } from '../Pop/GameGuide';
import { AudioMgr, SoundName } from '../Mgr/AudioMgr';
import { GameSet } from '../Pop/GameSet';
import { GameEnd } from '../Pop/GameEnd';
const { ccclass, property } = _decorator;

@ccclass('GameScene')
/**
 * 游戏场景
 */
export class GameScene extends Component {

    //
    static Instance: GameScene = null;

    protected onLoad(): void {
        GameScene.Instance = this;
        Global.IsEnd = false;
    }

    //手牌数量
    public HandNum: number = 3;

    //攻击牌信息
    public BattleCardInfo: CardType[] = [];
    //猫咪牌信息
    public CatCardInfo: CardType[] = [];

    //卡牌层
    @property(Node)
    public CardLoad: Node = null;

    //攻击层
    @property(Node)
    public CatCardLoad: Node = null;

    //卡牌数量
    @property(Label)
    public CardNum: Label = null;

    //弃牌
    @property(Node)
    public DisCard: Node = null;
    @property(Node)
    private DisCardBg: Node = null;

    //弃牌广告按钮
    @property(Node)
    private DisCardVideo: Node = null;

    //添加手牌广告按钮
    @property(Node)
    private AddCardVideo: Node = null;

    @property(Node)
    private UpdateButton: Node = null;

    protected start(): void {
        //初始化攻击牌信息
        for (let i = 0; i < 10; i++) {
            for (let j = 0; j < 4; j++) {
                this.BattleCardInfo.push({ color: j, num: i });
            }
        }

        //初始化猫咪牌信息
        for (let i = 0; i < 3; i++) {
            for (let j = 0; j < 4; j++) {
                this.CatCardInfo.push({ color: j, num: 10 + i });
            }
        }

        this.DisCardVideo.active = true;
        this.AddCardVideo.active = true;

        this.DisCard.active = false;
        this.DisCardBg.active = false;

        this.UpdateCardNum();

        this.CardLoad.getComponent(CardLoad).CheckAddCard();


        if (LocalStorageMgr.Instance.LOCAL_STORAGE_DATA.IS_FIRST_OPEN) {
            LocalStorageMgr.Instance.LOCAL_STORAGE_DATA.IS_FIRST_OPEN = false;
            LocalStorageMgr.Instance.UpdateLocalStroage();

            let gg: Node = UIMgr.Instance.ShowPop(AssetList.Pop.GAMEGUIDE)
            gg.getComponent(GameGuide).ShowNode1 = [
                { pos: this.CardLoad.getWorldPosition().add(new Vec3(-10, -150)), _rect: { width: 150, height: 214 } },
                { pos: this.CardLoad.getWorldPosition().add(new Vec3(-40, -80)), _rect: { width: 50, height: 50 } },
                { pos: this.CardLoad.getWorldPosition().add(new Vec3(-40, -80)), _rect: { width: 50, height: 50 } },
                { pos: this.CardLoad.getWorldPosition().add(new Vec3(-10, -150)), _rect: { width: 110, height: 110 } },
                { pos: this.UpdateButton.getWorldPosition(), _rect: { width: this.UpdateButton.getComponent(UITransform).width, height: this.UpdateButton.getComponent(UITransform).height } },
                { pos: this.AddCardVideo.getWorldPosition(), _rect: { width: this.AddCardVideo.getComponent(UITransform).width, height: this.AddCardVideo.getComponent(UITransform).height } },
                { pos: this.DisCardVideo.getWorldPosition(), _rect: { width: this.DisCardVideo.getComponent(UITransform).width, height: this.DisCardVideo.getComponent(UITransform).height } },
            ]
            gg.getComponent(GameGuide).Show();

        }
    }

    /**
     * 更新卡牌数量
     */
    public UpdateCardNum() {
        this.CardNum.string = this.BattleCardInfo.length.toString();
    }

    /**
    * 点击设置
    */
    private SetClick() {
        AudioMgr.instance.PlayEffect(SoundName.Click);
        let set: Node = UIMgr.Instance.ShowPop(AssetList.Pop.GAMESET);
        set.getComponent(GameSet).Init(false);
    }

    /**
     * 点击弃牌广告按钮
     */
    private DisVideoClick() {
        UIMgr.Instance.ShowTop(AssetList.Top.NoTouch);
        AdMgr.Instance.ShowVideo(0, (_isend: boolean) => {
            UIMgr.Instance.HideTop(AssetList.Top.NoTouch);

            if (_isend) {
                this.DisCardVideo.active = false;
                this.DisCard.active = true;
                this.DisCardBg.active = true;
            }


        })
    }

    /**
     * 添加手牌广告按钮
     */
    private AddVideoClick() {
        UIMgr.Instance.ShowTop(AssetList.Top.NoTouch);
        AdMgr.Instance.ShowVideo(0, (_isend: boolean) => {
            UIMgr.Instance.HideTop(AssetList.Top.NoTouch);

            if (_isend) {
                this.AddCardVideo.active = false;
                this.HandNum++;
                this.CardLoad.getComponent(CardLoad).GetCard();
                this.UpdateCardNum();
            }

        })
    }

    /**
     * 点击刷新按钮
     */
    private UpdateClick() {
        UIMgr.Instance.ShowTop(AssetList.Top.NoTouch);
        AdMgr.Instance.ShowVideo(0, (_isend: boolean) => {
            UIMgr.Instance.HideTop(AssetList.Top.NoTouch);
            if (_isend) {
                for (let card of this.CardLoad.getComponent(CardLoad).CardArray) {
                    this.BattleCardInfo.push(card.getComponent(BattleCard).CardInfo);
                    card.active = false;
                    this.CardLoad.getComponent(CardLoad).Nouse.push(card);
                }
                this.CardLoad.getComponent(CardLoad).CardArray = [];

                this.CardLoad.getComponent(CardLoad).CheckAddCard();
            }

        })
    }

    /**
     * 游戏结束
     */
    public GameOver() {
        Global.IsEnd = true;
        let ge: Node = UIMgr.Instance.ShowPop(AssetList.Pop.GAMEEND);
        ge.getComponent(GameEnd).UpdateScore(this.BattleCardInfo.length);
    }

}


